package com.hntxk.cqzd.export.service.impl;

import com.hntxk.core.framework.model.ResponseModel;
import com.hntxk.core.framework.service.impl.BaseServiceImpl;
import com.hntxk.cqzd.export.model.*;
import com.hntxk.cqzd.export.service.PatientInfoService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author liucz
 * @version 1.0.0
 * @ClassName PatientInfoServiceImpl.java
 * @Description TODO
 * @createTime 2021-01-14- 09:18:00
 */
@Service("patientInfo")
public class PatientInfoServiceImpl extends BaseServiceImpl<CmvParam> implements PatientInfoService {

    public static final String MAPPER_NAMESPACE="com.hntxk.cqzd.export.mapper.PatientInfoMapper.";

    public PatientInfoServiceImpl(){
        super(MAPPER_NAMESPACE);
    }

    @Override
    public List<CmvResult> getCmv(CmvParam model) {

        List<CmvResult>  list
             = getDasTemplate().selectList(MAPPER_NAMESPACE + "getCmv", model);

        if (! CollectionUtils.isEmpty(list)){
            list = this.handleInfo(list);
        }

        return list;

    }

    @Override
    public List<PunctureResult> getPuncture(CmvParam model) {
        return getDasTemplate().selectList(MAPPER_NAMESPACE + "getPuncture", model);
    }

    @Override
    public List<BaseInfoResult> getBaseInfo(CmvParam model) {
        return getDasTemplate().selectList(MAPPER_NAMESPACE + "getBaseInfo", model);
    }

    @Override
    public List<FollowUpInfoResult> getFollowUpInfo(CmvParam model) {
        return getDasTemplate().selectList(MAPPER_NAMESPACE + "getFollowUpInfo", model);
    }

    @Override
    public List<FruitResultModel> getFruit(Integer diagnosisId) {
        return getDasTemplate().selectList(MAPPER_NAMESPACE + "getFruit", diagnosisId);
    }

    @Override
    public List<String> getRemark(Integer diagnosisId) {
        return getDasTemplate().selectList(MAPPER_NAMESPACE + "getRemark", diagnosisId);
    }


    @Override
    public List<CmvResult> handleInfo(List<CmvResult>  list) {
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(handleCmv -> {
                Integer diagnosisId = handleCmv.getDiagnosisId();
                List<FruitResultModel> fruit = this.getFruit(diagnosisId);
                List<String> remark = this.getRemark(diagnosisId);
                if (!CollectionUtils.isEmpty(remark)) {
                    String value = "";

                    for (String s : remark) {
                        if (!StringUtils.isEmpty(s)) {
                            value += s;
                        }

                    }
                    if (!StringUtils.isEmpty(value)) {
                        handleCmv.setDiacrisisRemark(value);
                    }

                }
                if (!CollectionUtils.isEmpty(fruit)) {
                    fruit.forEach(handleFruit -> {
                        switch (handleFruit.getFruitTypeName()) {
                            case "核型分析":
                                if (!StringUtils.isEmpty(handleFruit.getFruitExplain())) {
                                    handleCmv.setKaryoType(handleFruit.getFruitExplain());
                                }
                                break;
                            case "FISH":
                                if (!StringUtils.isEmpty(handleFruit.getFruitExplain())) {
                                    handleCmv.setFish(handleFruit.getFruitExplain());
                                }
                                break;
                            case "CNV":
                                if (!StringUtils.isEmpty(handleFruit.getFruitExplain())) {
                                    handleCmv.setCnv(handleFruit.getFruitExplain());
                                }
                                break;
                            case "QF-PCR":
                                if (!StringUtils.isEmpty(handleFruit.getFruitExplain())) {
                                    handleCmv.setQfPcr(handleFruit.getFruitExplain());
                                }
                                break;
                            case "其他":
                                if (!StringUtils.isEmpty(handleFruit.getFruitExplain())) {
                                    handleCmv.setOther(handleFruit.getFruitExplain());
                                }
                                break;
                            default:
                                break;
                        }
                    });
                }

            });

        }

        return list;
    }

    @Override
    public ResponseModel getAllDiagnosisInfo(AllDiagnosisInfoPar model) {
        List<AllDiagnosisInfo> list=null;
        ResponseModel responseModel =ResponseModel.empty();
        if (model.getState()==1){
            responseModel = super.query2Json(MAPPER_NAMESPACE + "allDiagnosisInfo", model, false);
            list=(List<AllDiagnosisInfo>)responseModel.getData();
        }else if (model.getState()==2){
             list=getDasTemplate().selectList(MAPPER_NAMESPACE + "allDiagnosisInfo",model);
        }
        if (!CollectionUtils.isEmpty(list)) { 
            list.forEach(mode->{
                // 诊断信息ID
                Integer diacrisisId = mode.getDiacrisisId();
                //查询患者孩子数量
                Integer children =getDasTemplate().selectOne(MAPPER_NAMESPACE + "queryChildren", diacrisisId);
                mode.setChildren(children);
                Integer diagnosisId = mode.getDiagnosisId();
                // 得到术前情况
                List<String> remark = this.getRemark(diagnosisId);
                if (!CollectionUtils.isEmpty(remark)) {
                    String value = "";

                    for (String s : remark) {
                        if (!StringUtils.isEmpty(s)) {
                            value += s;
                        }

                    }
                    if (!StringUtils.isEmpty(value)) {
                        mode.setDiacrisisRemark(value);
                    }

                }
                // 拿到检验信息ID的集合
                List<Integer> diacrisisInfoList = getDasTemplate().selectList(MAPPER_NAMESPACE + "diacrisisInfoList", diacrisisId);
                diacrisisInfoList.forEach(dia->{
                    TblDiagnosisInfoPar info = getDasTemplate().selectOne(MAPPER_NAMESPACE + "queryDiagnosisInfo", dia);
                    List<FruitResultModel> fruit = null;
                    switch (info.getInspectionItemId()){
                        case 1:
                            fruit=this.getFruit(dia);
                            mode.setBsignYc(info.getBSign());
                            mode.setAfTypeNameYc(info.getAfTypeName());
                            mode.setInspectionProcessYc(info.getInspectionProcess());
                            if (!CollectionUtils.isEmpty(fruit)) {
                                fruit.forEach(handleFruit -> {
                                    switch (handleFruit.getFruitTypeName()) {
                                        case "核型分析":
                                            if (!StringUtils.isEmpty(handleFruit.getFruitExplain())) {
                                                mode.setKaryotypingYc(handleFruit.getFruitExplain());
                                            }
                                            break;
                                        case "FISH":
                                            if (!StringUtils.isEmpty(handleFruit.getFruitExplain())) {
                                                mode.setFishYc(handleFruit.getFruitExplain());
                                            }
                                            break;
                                        case "CNV":
                                            if (!StringUtils.isEmpty(handleFruit.getFruitExplain())) {
                                                mode.setCnvYc(handleFruit.getFruitExplain());
                                            }
                                            break;
                                        case "QF-PCR":
                                            if (!StringUtils.isEmpty(handleFruit.getFruitExplain())) {
                                                mode.setQfPcrYc(handleFruit.getFruitExplain());
                                            }
                                            break;
                                        default:
                                            break;
                                    }
                                });
                            }
                            break;
                        case 2:
                            fruit=this.getFruit(dia);
                            mode.setBsignQc(info.getBSign());
                            mode.setAfTypeNameQc(info.getAfTypeName());
                            mode.setInspectionProcessQc(info.getInspectionProcess());
                            if (!CollectionUtils.isEmpty(fruit)) {
                                fruit.forEach(handleFruit -> {
                                    switch (handleFruit.getFruitTypeName()) {
                                        case "核型分析":
                                            if (!StringUtils.isEmpty(handleFruit.getFruitExplain())) {
                                                mode.setKaryotypingQc(handleFruit.getFruitExplain());
                                            }
                                            break;
                                        case "FISH":
                                            if (!StringUtils.isEmpty(handleFruit.getFruitExplain())) {
                                                mode.setFishQc(handleFruit.getFruitExplain());
                                            }
                                            break;
                                        case "CNV":
                                            if (!StringUtils.isEmpty(handleFruit.getFruitExplain())) {
                                                mode.setCnvQc(handleFruit.getFruitExplain());
                                            }
                                            break;
                                        case "QF-PCR":
                                            if (!StringUtils.isEmpty(handleFruit.getFruitExplain())) {
                                                mode.setQfPcrQc(handleFruit.getFruitExplain());
                                            }
                                            break;
                                        default:
                                            break;
                                    }
                                });
                            }
                            break;
                        case 3:
                            fruit=this.getFruit(dia);
                            mode.setBsignRc(info.getBSign());
                            mode.setAfTypeNameRc(info.getAfTypeName());
                            mode.setInspectionProcessRc(info.getInspectionProcess());
                            if (!CollectionUtils.isEmpty(fruit)) {
                                fruit.forEach(handleFruit -> {
                                    switch (handleFruit.getFruitTypeName()) {
                                        case "核型分析":
                                            if (!StringUtils.isEmpty(handleFruit.getFruitExplain())) {
                                                mode.setKaryotypingRc(handleFruit.getFruitExplain());
                                            }
                                            break;
                                        case "FISH":
                                            if (!StringUtils.isEmpty(handleFruit.getFruitExplain())) {
                                                mode.setFishRc(handleFruit.getFruitExplain());
                                            }
                                            break;
                                        case "CNV":
                                            if (!StringUtils.isEmpty(handleFruit.getFruitExplain())) {
                                                mode.setCnvRc(handleFruit.getFruitExplain());
                                            }
                                            break;
                                        case "QF-PCR":
                                            if (!StringUtils.isEmpty(handleFruit.getFruitExplain())) {
                                                mode.setQfPcrRc(handleFruit.getFruitExplain());
                                            }
                                            break;
                                        default:
                                            break;
                                    }
                                });
                            }
                            break;

                        default:
                            break;
                    }
                });
            });
        }
        return  responseModel.setData(list);
    }

}
